package net.slashie.libjcsi.examples.luck;

import net.slashie.libjcsi.CSIColor;
import net.slashie.libjcsi.wswing.*;

/**
 *Version 3.3, 04/09/08,
 *Same as 3.1 but works on JWS.
 *Originally made for the 1st under 1KBRL Challenge
 *
 * Modified to work with the CSIColor construct
 * @author Santiago Zapata
 */
class Luck {

    private static final int MAP_DIMENSIONS = 20;
	int xCoordinate, yCoordinate, c, d, xCoordinatesForMonster, yCoordinatesForMonster, xCoordsForFood, yCoordsForFood, xCoordsForExit, yCoordsForExit, level, r, keyPressed, turnsLeft = 600, moveMonsterX, moveMonsterY;
    boolean map[][];

    int randomD18() {
        return (int) (Math.random() * 17 + 1);
    }
    int[] s = new int[]{0, 0, -1, 1, -1, 1, 0, 0};

    void paintDiamondShapeFielfOfView(int x, int y, char h) {
        console.print(x, y, distanceFromCoordinate(x, y) < 7 ? h : ' ', CSIColor.DEFAULT_PALLET[1 + level % 13]);
    }

	private int distanceFromCoordinate(int x, int y) {
		return Math.abs(xCoordinate - x) + Math.abs(yCoordinate - y);
	}

    public static void main(String[] p) {
        new Luck();
    }

    Luck() {
        console.cls();
        for (;;) {
            if (xCoordsForExit == xCoordinate & yCoordsForExit == yCoordinate) {
                loadMap();
                xCoordsForExit = randomD18();
                yCoordsForExit = randomD18();
                xCoordinate = randomD18();
                yCoordinate = randomD18();
                xCoordsForFood = randomD18();
                yCoordsForFood = randomD18();
                level++;
                map[xCoordsForExit][yCoordsForExit] = map[xCoordsForFood][yCoordsForFood] = false;
            }
            if (xCoordsForFood == xCoordinate & yCoordsForFood == yCoordinate) {
                xCoordsForFood = 40;
                r++;
            }
            for (int x = 0; x < 19; x++) {
                for (int y = 0; y < 19; y++) {
                    paintDiamondShapeFielfOfView(x, y, map[x][y] ? '#' : '.');
                }
            }
            console.print(2, 22, "L" + level + " E" + turnsLeft + " R" + r + "  ", 7);
            console.print(xCoordinate, yCoordinate, "@", 12);
            paintDiamondShapeFielfOfView(xCoordsForExit, yCoordsForExit, '>');
            paintDiamondShapeFielfOfView(xCoordsForFood, yCoordsForFood, '%');
            paintDiamondShapeFielfOfView(xCoordinatesForMonster, yCoordinatesForMonster, '&');
            console.refresh();
            keyPressed = console.inkey().code;
            d = s[keyPressed % 4];
            c = s[4 + keyPressed % 4];
            if (xCoordinate + d == xCoordinatesForMonster & yCoordinate + c == yCoordinatesForMonster) {
                if (randomD18() > 8) {
                    xCoordinatesForMonster = randomD18();
                    yCoordinatesForMonster = randomD18();
                }
            } else if (!map[xCoordinate + d][yCoordinate + c]) {
                xCoordinate += d;
                yCoordinate += c;
            }
            moveMonsterX = Integer.signum(xCoordinate - xCoordinatesForMonster);
            moveMonsterY = Integer.signum(yCoordinate - yCoordinatesForMonster);
            if (xCoordinatesForMonster + moveMonsterX == xCoordinate & yCoordinatesForMonster + moveMonsterY == yCoordinate) {
                turnsLeft -= 5;
            } else if (turnsLeft % (5 - (int) (level / 4.5d)) == 0 & !map[xCoordinatesForMonster + moveMonsterX][yCoordinatesForMonster + moveMonsterY]) {
                xCoordinatesForMonster += moveMonsterX;
                yCoordinatesForMonster += moveMonsterY;
            }
            if (--turnsLeft < 0 | level > 20) {
                break;
            }
        }
        console.print(2, 23, r > 9 & level > 20 ? "WON" : "DIE", 4);
        console.refresh();
        console.waitKey(40);
    }

	private void loadMap() {
		map = new boolean[MAP_DIMENSIONS][MAP_DIMENSIONS];
		for (keyPressed = 0; ++keyPressed < 20;) {
		    map[randomD18()][randomD18()] = map[0][keyPressed] = map[19][keyPressed] = map[keyPressed][0] = map[keyPressed][19] = true;
		}
	}
	
    WSwingConsoleInterface console = new WSwingConsoleInterface("LUCK - libjcsi Testing Grounds", true);
}